function formatNumber(n) {
    const str = n.toString()
    return str[1] ? str : `0${str}`
}

var baseUrl = 'https://api.wugengren.com'
export const http = {
    get(url, data) { //get方法
        wx.showLoading({
            title: '加载中'
        })
        return new Promise((resolve, reject) => {
            console.log(data)
            wx.request({
                url: baseUrl + url,
                data: data,
                method: 'GET',
                header: {
                    "headerUserToken": "wx-08db1ce041d943d0acd52bd8ab8b4e63"
                },
                success: function (res) {
                    wx.hideLoading();
                    if (res.statusCode != 200) {
                        wx.showToast({
                            title: "网络出错，稍后再试",
                            icon: "none"
                        });
                        return false;
                    }
                    resolve(res.data);
                },
                fail: function (error) {
                    // fail
                    wx.hideLoading();
                    reject(error); //请求失败
                },
                complete: function () {
                    // wx.hideLoading();
                    // complete
                }
            })
        })
    },
    post(url, data, method) { //post方法
        wx.showLoading({
            title: '加载中'
        })
        return new Promise((resolve, reject) => {
            wx.request({
                url: baseUrl + url,
                data: data,
                method: method ? method : 'post',
                header: {
                    "headerUserToken": "wx-08db1ce041d943d0acd52bd8ab8b4e63",
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                success: function (res) {
                    // success
                    wx.hideLoading();
                    if (res.statusCode != 200) {
                        wx.showToast({
                            title: "网络出错，稍后再试",
                            icon: "none"
                        });
                        return false;
                    }
                    resolve(res.data);
                },
                fail: function (error) {
                    // fail
                    wx.hideLoading();
                    reject(error); //请求失败
                },
                complete: function () {
                    // wx.hideLoading();
                    // complete
                }
            })
        })
    }
}
export function formatTime(date) {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()

    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    const t1 = [year, month, day].map(formatNumber).join('/')
    const t2 = [hour, minute, second].map(formatNumber).join(':')

    return `${t1} ${t2}`
}

export default {
    formatNumber,
    formatTime
}